package com.tencent.apollo;

import android.Manifest;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import android.content.Context;
import android.util.Log;
import java.security.MessageDigest;
import java.util.Locale;
import android.provider.Settings.Secure;
import android.telephony.TelephonyManager;
import android.content.Context;
import android.content.pm.PackageManager;
import android.support.v4.content.ContextCompat;

public class ApolloVoiceUDID {

	private static String LOGTAG = "GCloudVoiceTag";
	
	private static Context mainContext;
	private static final String DefaultUUID = "UUID";
    
    public static void SetContext(Context ctxt) {
    	mainContext = ctxt;
    }

    // ******************************************************************
    //					UUID
    // ******************************************************************
    public static String GetDeviceID(Context context)
    {
    	int hasPermission = ContextCompat.checkSelfPermission(context, Manifest.permission.READ_PHONE_STATE);
        if (hasPermission != PackageManager.PERMISSION_GRANTED) 
        {
        	Log.e(LOGTAG, "getDeviceID, Permission Denied. ");  
            return "";
        }
        
        String imeistring = null;
        try
    	{
        	TelephonyManager telephonyManager;
        	telephonyManager =(TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
        	if(telephonyManager == null)
        	{
        		return "";
        	}
        	
        	// !!! require permissions READ_PHONE_STATE
        	imeistring = telephonyManager.getDeviceId();
        	
        	return imeistring;
    	}
    	catch(Exception e)
    	{
    		Log.e(LOGTAG, "get DeviceID failed: " + e.toString());  
    	}

        return "";
    }
	
	public static String UDID() {

        String deviceId = GetDeviceID(mainContext);
        String serial = android.os.Build.SERIAL;
        String androidId = Secure.getString(mainContext.getContentResolver(), Secure.ANDROID_ID);
    	
    	StringBuilder builder = new StringBuilder();
    	String flag = "%";
    	if(null != deviceId)
    	{
    		builder.append(flag);
    		builder.append(deviceId);
    	}
    	if(null != serial)
    	{
    		builder.append(flag);
    		builder.append(serial);
    	}
    	if(null != androidId)
    	{
    		builder.append(flag);
    		builder.append(androidId);
    	}
    	
    	String uuid = builder.toString();
    	
    	//all failed ,return default UUID
    	if(0 == uuid.length())
    	{
    		return DefaultUUID;
    	}
    	
    	try
    	{
    		//get Algorithm MD5 Instance
    		MessageDigest digest = java.security.MessageDigest.getInstance("MD5");
        	if(null == digest)
        	{
        		return DefaultUUID;
        	}
        	digest.update(uuid.getBytes());
    		byte hash[] = digest.digest();
    		
    		//MD5 to Hex UpperCase
    		StringBuilder hex = new StringBuilder();
    		for(int i=0; i<hash.length;i++)
    		{
    			hex.append(Integer.toHexString(0xFF & hash[i]));
    		}
    		return hex.toString().toUpperCase(Locale.ENGLISH);
    	}
    	catch (Exception e) 
 		{
 			Log.e(LOGTAG,"GetUUID Exception:" + e);
    		return DefaultUUID;
    	}
    }
}
